package au.edu.unsw.editingtool.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import au.edu.unsw.editingtool.global.GlobalConstant;
import au.edu.unsw.editingtool.global.Metadata;

import java.util.*;

/**
 * Servlet implementation class Preview
 */
public class Preview extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private String ApplicationPath = null;
	
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Preview() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
		super.init();
	}

	/**
	 * @see Servlet#getServletConfig()
	 */
	public ServletConfig getServletConfig() {
		// TODO Auto-generated method stub
		return super.getServletConfig();
	}

	/**
	 * @see Servlet#getServletInfo()
	 */
	public String getServletInfo() {
		// TODO Auto-generated method stub
		return super.getServletInfo();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doService(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doService(request, response);
	}
	protected void doService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ApplicationPath = request.getContextPath();
		System.out.println("==== [PREVIEW Servlet] ====");
		
		HttpSession session = request.getSession(false);
		Hashtable <String,String> tb = (Hashtable<String, String>) session.getAttribute(GlobalConstant.requestFormData);
		
		//Update Hashtable from the parameter in the form.
		Enumeration keys = request.getParameterNames();
		while(keys.hasMoreElements())
		{
			String key = (String) keys.nextElement();
			tb.put(key, request.getParameter(key));			
		}		
		String pid = null;
		pid = (String) request.getAttribute(GlobalConstant.pid);
		if (pid ==null)
			pid = request.getParameter(GlobalConstant.pid);
		
		session.setAttribute(GlobalConstant.requestLastCommand,GlobalConstant.cmdPreview);		
		session.setAttribute(GlobalConstant.requestFormData,tb);
		request.setAttribute(GlobalConstant.pid,pid);
		request.setAttribute(GlobalConstant.requestFormDataJS,Metadata.transformHashToJavaScriptAssociativeArray(tb));				
		if(session.getAttribute(GlobalConstant.requestFormData)==null)
		{
			System.out.println("[PREVIEW] FORMDATA IS NULL!");
			System.out.println("[PREVIEW] forward to "+GlobalConstant.JSPError);
			RequestDispatcher rd = request.getRequestDispatcher(GlobalConstant.JSPError);
			rd.forward(request, response);			
		}
		
		for(Enumeration sesskeys = session.getAttributeNames();sesskeys.hasMoreElements();)
		{
			String sesskey = (String) sesskeys.nextElement();
			System.out.println("[PREVIEW] Preview - Key : "+sesskey+" and Value : "+session.getAttribute(sesskey.toString()));
		}
		System.out.println("[PREVIEW] forward to "+GlobalConstant.JSPPreview);
		RequestDispatcher rd = request.getRequestDispatcher(GlobalConstant.JSPPreview);
		rd.forward(request, response);
		
	}

}
